package com.omega.controller;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.omega.common.utils.Utils;
import com.omega.constants.ApplicationConstants;
import com.omega.entity.ItemDenomination;
import com.omega.service.ItemDenominationService;
import com.omega.vo.ItemDenominationList;

@Controller
@RequestMapping(value = "/admin/denomination")
public class ItemDenominationController {

	@Autowired
	ItemDenominationService itemDenominationService;
	
	@Autowired
	ItemDenominationList denominationList;

	@RequestMapping(value = "/addDenomination", method = RequestMethod.GET)
	public String showAddDenominationForm(Model model) {
		List<ItemDenomination> list = new CopyOnWriteArrayList<ItemDenomination>();
		for(int i = 0; i<5; i++){
			list.add(new ItemDenomination());
		}
		denominationList.setDenominationList(list);
		model.addAttribute("itemDenominationListForm",
				denominationList);
		model.addAttribute("yearMap", Utils.getYearsMap());
		model.addAttribute("monthMap", Utils.getMonthsMap());
		return "addDenomination";
	}

	@RequestMapping(value = "/addDenomination", method = RequestMethod.POST)
	public String submitAddDenominatioForm(
			@ModelAttribute("itemDenominationListForm") ItemDenominationList denominationListForm,
			BindingResult result, Model model, HttpServletRequest request) {
		for(ItemDenomination form : denominationListForm.getDenominationList()){
			if(!(StringUtils.isEmpty(form.getDenominationName()))){
				itemDenominationService.addDenomination(form);
			}	
		}
		return "redirect:/admin/denomination/viewDenominations";
	}

	@RequestMapping(value = "/viewDenominations", method = RequestMethod.GET)
	public String viewAllDenominations(Model model) {
		model.addAttribute(ApplicationConstants.DENOMINATIONS_LIST_VO,
				itemDenominationService.loadAllDenominations());
		return "viewDenominations";
	}

	@RequestMapping(value = "/editDenomination/{denominationId}", method = RequestMethod.GET)
	public String showEditDenominationForm(@PathVariable String denominationId,
			Model model) {
		ItemDenomination denomination = itemDenominationService.loadById(Long
				.parseLong(denominationId));
		model.addAttribute(ApplicationConstants.DENOMINATION_VO, denomination);
		model.addAttribute("yearMap", Utils.getYearsMap());
		model.addAttribute("monthMap", Utils.getMonthsMap());
		return "editDenomination";
	}

	@RequestMapping(value = "/editDenomination/{denominationId}", method = RequestMethod.POST)
	public String submitEditSupplierForm(
			@PathVariable String denominationId,
			@ModelAttribute(ApplicationConstants.DENOMINATION_VO) ItemDenomination form,
			BindingResult result, Model model) {
		itemDenominationService.updateDenomination(form);
		return "redirect:/admin/denomination/viewDenominations";
	}

	@RequestMapping(value = "/deactivateDenomination/{denominationId}", method = RequestMethod.GET)
	public String deactivateDenomination(
			@PathVariable String denominationId,
			Model model) {
		itemDenominationService.deactivateDenomination(Long.parseLong(denominationId));
		return "redirect:/admin/denomination/viewDenominations";
	}
	
	@RequestMapping(value = "/activateDenomination/{denominationId}", method = RequestMethod.GET)
	public String activateDenomination(
			@PathVariable String denominationId,
			Model model) {
		itemDenominationService.activateDenomination(Long.parseLong(denominationId));
		return "redirect:/admin/denomination/viewDenominations";
	}

}
